package kr.hkit.summart_java.serviceUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.BoxLayout;

import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summart_java.dao.CodeTitleDAO;
import kr.hkit.summart_java.dao.DaoInterface;
import kr.hkit.summart_java.erp.dto.CodeTitle;
import kr.hkit.summart_java.erp.dto.Department;

import javax.swing.JButton;
import javax.swing.JLabel;

public class PanelCodeTitleUI extends JPanel implements ActionListener {
	private InputPanel ptitleno;
	private InputPanel ptitlename;
	private DaoInterface<CodeTitle> dao;
	public static JButton btnadd1;
	public static JButton btndel1;
	public static JButton btnupd1;
	public static JButton btnsear1;
	private JLabel label;

	/**
	 * Create the panel.
	 */
	public PanelCodeTitleUI() {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		JPanel panel = new JPanel();
		add(panel);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
		ptitleno = new InputPanel("직급번호");
		panel.add(ptitleno);
		
		ptitlename = new InputPanel("직급명");
		panel.add(ptitlename);
		
		label = new JLabel("");
		panel.add(label);
		
		JPanel panel_1 = new JPanel();
		add(panel_1);
		
		btnadd1 = new JButton("");
		btnadd1.setEnabled(true);
		btnadd1.addActionListener(this);
		panel_1.add(btnadd1);
		

	}


	public PanelCodeTitleUI(CodeTitleDAO dao3) {
		// TODO Auto-generated constructor stub
	}

	public void actionPerformedbtnadd1(ActionEvent e) {
		int no = Integer.parseInt(ptitleno.getTf().trim());
		String name = ptitlename.getTf();
		
		CodeTitle c = new CodeTitle(no, name);
		JOptionPane.showMessageDialog(this, c);
		try {
			dao.insertItem(c);
			JOptionPane.showMessageDialog(this, "입력되었습니다.");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "입력실패.");
		}
	}
	public void actionPerformedbtndel1(ActionEvent e) {
		int no = Integer.parseInt(ptitleno.getTf().trim());
		String name = ptitlename.getTf();
		
		CodeTitle c = new CodeTitle(no, name);
		JOptionPane.showMessageDialog(this, c);
		try {
			dao.deleteItemByNo(c);
			JOptionPane.showMessageDialog(this, "삭제되었습니다.");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "삭제실패.");
		}
	}
	public void actionPerformedbtnupd1(ActionEvent e) {
		int no = Integer.parseInt(ptitleno.getTf().trim());
		String name = ptitlename.getTf();
		
		CodeTitle c = new CodeTitle(no, name);
		JOptionPane.showMessageDialog(this, c);
		try {
			dao.updateItemByNo(c);
			JOptionPane.showMessageDialog(this, "수정되었습니다.");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "수정실패.");
		}
	}
	public void actionPerformedbtnsear1(ActionEvent e) {
		CodeTitle item = null;
		int no = Integer.parseInt(ptitleno.getTf().trim());
		String name = ptitlename.getTf();
		
		CodeTitle c = new CodeTitle(no,name);
		JOptionPane.showMessageDialog(this, c);
		try {
			dao.searchItem(c);
			item = dao.searchItem(c);
			label.setText(item+"");
			JOptionPane.showMessageDialog(this, "검색되었습니다.");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "실패.");
		}
	}

	public void setDao(DaoInterface<CodeTitle> dao) {
		this.dao = dao;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(btnadd1.getText()=="추가"){
			actionPerformedbtnadd1(e);
		}else if(btnadd1.getText()=="삭제"){
			actionPerformedbtndel1(e);
		}else if(btnadd1.getText()=="수정"){
			actionPerformedbtnupd1(e);
		}else{
			actionPerformedbtnsear1(e);
		}

	}
}